package com.Lixiaoqian.Gulangyu.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * 检查权限的工具类
 * Created by Fsh on 2016/12/29.
 */

public class PermissionsChecker {

    // 判断权限集合
    public static void checkAndOpenPermiss(Activity pActivity, String... permissions) {
        for(String permission : permissions) {
            if(lacksPermission(pActivity, permission)) {
               OpenPermiss(pActivity,permissions);
            }
        }
    }

    /**
     * 判断是否缺少权限
     * @param mContext
     * @param permission
     * @return
     */
    private static boolean lacksPermission(Activity mContext, String permission) {
        if(Build.VERSION.SDK_INT > 22) {
            return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED;
        }else{
            return false;
        }

    }

    /**
     * 检测并开启
     * @param context
     * @param permissions 权限列表
     */
    private static void OpenPermiss(Activity context, String... permissions) {
        if(Build.VERSION.SDK_INT > 22) {
            ActivityCompat.requestPermissions(context, permissions, 1);
        }
    }
}

